0

@Oleg - I am new to jqGrid.I have three issues. Urgent help required. I am using jqGrid 3.8, inline edit mode.

  1. I want to retain the invalid cell values after the pop up for invalid cell.
  2. Also I want to set the focus back to the invalid cell.
  3. I have "add row" and filter tool bar feature in my jqGrid. I have used Oleg's solution in creating drop down for filter tool bar (posted in another jQuery thread).

** - Problem:

** I am calling setSearchSelect from afterSaveCell, because I want to add new values in filter drop down every time I add or delete column.(Note: column is a textbox). But the filter tool bar isn't getting refreshed even if I use

var sgrid = $("#list")[0];
sgrid.triggerToolbar();

See the code below for setting the toolbar.

<script type="text/javascript">
var mydata = [
        {id:"1", Name:"Miroslav Klose",     Category:"sport",   Subcategory:"football"},
        {id:"2", Name:"Michael Schumacher", Category:"sport",   Subcategory:"formula 1"},
        {id:"3", Name:"Albert Einstein",    Category:"science", Subcategory:"physics"},
        {id:"4", Name:"Blaise Pascal",      Category:"science", Subcategory:"mathematics"}
    ],
    grid = $("#list"),
    getUniqueNames = function(columnName) {
        var texts = grid.jqGrid('getCol',columnName), uniqueTexts = [],
            textsLength = texts.length, text, textsMap = {}, i;
        for (i=0;i<textsLength;i++) {
            text = texts[i];
            if (text !== undefined && textsMap[text] === undefined) {
                // to test whether the texts is unique we place it in the map.
                textsMap[text] = true;
                uniqueTexts.push(text);
            }
        }
        return uniqueTexts;
    },
    buildSearchSelect = function(uniqueNames) {
        var values=":All";
        $.each (uniqueNames, function() {
            values += ";" + this + ":" + this;
        });     
        return values;
    },
    setSearchSelect = function(columnName) {    
        grid.jqGrid('setColProp', columnName,
                    {
                        stype: 'select',
                        searchoptions: {
                            value:buildSearchSelect(getUniqueNames(columnName)),
                            sopt:['eq']
                        }
                    }
        );      
    };

grid.jqGrid({
    data: mydata,
    datatype: 'local',
    colModel: [
        { name:'Name', index:'Name', width:200 ,editable:true},
        { name:'Category', index:'Category', width:200,editable:true },
        { name:'Subcategory', index:'Subcategory', width:200,editable:true }
    ],
    sortname: 'Name',
    viewrecords: true,
    rownumbers: true,
    sortorder: "desc",
    editurl: "clientArray",
    multiselect: true,
    pagination:true,
    cellEdit: true,
    cellsubmit: 'clientArray',
    //ignoreCase: true,
    pager: '#pager',
    height: "auto",
    enableSearch: true,
    caption: "How to use filterToolbar better locally", 
    afterSaveCell: function(rowid,name,val,iRow,iCol) { 
        setSearchSelect(name);  

        jQuery("#list").('setColProp', name,
                    {
                       width:100
                    }
        );
        var sgrid = $("#list")[0];
        sgrid.triggerToolbar();
alert(name);        
    },
    loadComplete: function () {
                    setSearchSelect('Category');    
                }
}).jqGrid('navGrid','#pager',
          {edit:false, add:false, del:false, search:false, refresh:true});

setSearchSelect('Category');
setSearchSelect('Subcategory');

grid.jqGrid('setColProp', 'Name',
            {
                searchoptions: {
                    sopt:['cn'],
                    dataInit: function(elem) {
                        $(elem).autocomplete({
                            source:getUniqueNames('Name'),
                            delay:0,
                            minLength:0
                        });
                    }
                }
            });

grid.jqGrid('filterToolbar',
            {stringResult:true, searchOnEnter:true, defaultSearch:"cn"});


function addRow(tableId){
    var loopRow = document.getElementById("addRowsInput").value;                
    var recordCount = '';
    var rwData = '';
    //var selRowIds = getRowIDs('list');
    var gridProducts =  $("#list"); 
    var resetFirstRow = jQuery("#list").getRowData( 1 );
    jQuery("#list").setRowData( 1, resetFirstRow );
    if(loopRow == null || loopRow == "" || loopRow == "Enter number of units to be added")
    {
        loopRow = 1;
    }

        for(i=0; i< loopRow; i++)
        {       
                recordCount = jQuery("#list").getGridParam("records") ;
                var emptydata = [
                    {id:(recordCount+1), Name:"",     Category:"",   Subcategory:""}]
                    gridProducts.jqGrid('addRowData', recordCount+1, emptydata[0]);                                 
        }
    }
</script>

@Oleg - one more question on the solution you suggested. Sorry I tried myself to find it but couldn't. In the buildSearchSelect: method , how can I include filter for empty string. As explained above I have a "Add Row" button. So when the user wants to filter rows with empty columns I need a filter value.

Sree
  • 13
  • 8
  • Update:I am using datatype:"local", cellSubmit:"clientArray" – Sree May 11 '11 at 07:19
  • You don't posted your main JavaScript code. The codes which you posted come from [the answer](http://stackoverflow.com/questions/5328072/can-jqgrid-support-dropdowns-in-the-toolbar-filter-fields/5329014#5329014) and [the demo](http://www.ok-soft-gmbh.com/jqGrid/FillToolbarSearchFilter.htm). Could you modify the text of your question and include the whole JavaScript code which you use? – Oleg May 11 '11 at 08:14
  • Thanks for replying Oleg. PFB my script code – Sree May 11 '11 at 08:36
  • @Oleg - Thanks for replying Oleg. I just used your filtertoolbar example with editable option. Please find my script code in the modified question – Sree May 11 '11 at 08:40
  • @user746147: Sorry, but what means "PFB"? – Oleg May 11 '11 at 08:41
  • @user746147: I found the code. – Oleg May 11 '11 at 08:44
  • @Oleg -PFB means "Please Find Below". Sorry I tried to paste the – Sree May 11 '11 at 08:48
  • @user746147: OK! I will look at the code. If you will select the code and click "{}" button in the editing toolbar all lines of the code will be started with 4 additional blanks and an empty line could be added before the code. After that the code will be good formatted and displayed. – Oleg May 11 '11 at 08:52
  • @Oleg - Oh thanks. Sorry I am a new user. So i didn't know. – Sree May 11 '11 at 08:54

1 Answers1

0

The implementation of setSearchSelect function from my old answer work only if the searching toolbar not yet exist. If the toolbar exist one have to modify the options of the select element or autocomplete source of the jQuery UI autocomplete widget.

I extended the code. You can see the new version of the demo here. In the same way one could use inline editing instead of the cell editing.

Here is the modified JavaScript code:

var mydata = [
        {id:"1", Name:"Miroslav Klose",     Category:"sport",   Subcategory:"football"},
        {id:"2", Name:"Michael Schumacher", Category:"sport",   Subcategory:"formula 1"},
        {id:"3", Name:"Albert Einstein",    Category:"science", Subcategory:"physics"},
        {id:"4", Name:"Blaise Pascal",      Category:"science", Subcategory:"mathematics"}
    ],
    grid = $("#list"),
    getUniqueNames = function(columnName) {
        var texts = grid.jqGrid('getCol',columnName), uniqueTexts = [],
            textsLength = texts.length, text, textsMap = {}, i;
        for (i=0;i<textsLength;i++) {
            text = texts[i];
            if (text !== undefined && textsMap[text] === undefined) {
                // to test whether the texts is unique we place it in the map.
                textsMap[text] = true;
                uniqueTexts.push(text);
            }
        }
        uniqueTexts.sort();
        return uniqueTexts;
    },
    buildSearchSelect = function(uniqueNames) {
        var values=":All";
        $.each (uniqueNames, function() {
            values += ";" + this + ":" + this;
        });
        return values;
    },
    setSearchSelect = function(columnName) {
        var $select = $('select#gs_'+columnName), un = getUniqueNames(columnName),
            htmlForSelect = '<option value="">All</option>', i, l=un.length, val;
        grid.jqGrid('setColProp', columnName,
                    {
                        stype: 'select',
                        searchoptions: {
                            value:buildSearchSelect(un),
                            sopt:['eq']
                        }
                    }
        );
        if ($select.length > 0) {
            // The searching toolbar already exist. One have to update it manually
            for (i=0;i<l;i++) {
                val = un[i];
                htmlForSelect += '<option value="'+val+'">'+val+'</option>';
            }
            $select.html(htmlForSelect);
        }
    },
    setAutocomplete = function(columnName) {
        var $input = $('input#gs_'+columnName), un = getUniqueNames(columnName);
        grid.jqGrid('setColProp', columnName,
                    {
                        searchoptions: {
                            sopt:['cn'],
                            dataInit: function(elem) {
                                $(elem).autocomplete({
                                    source:un,
                                    delay:0,
                                    minLength:0
                                });
                            }
                        }
                    });
        if ($input.length > 0) {
            // The searching toolbar already exist. One have to update the source
            $input.autocomplete('option', 'source', un);
        }
    },
    selectColumns = ['Category','Subcategory'], autocompleteColumns = ['Name'];

grid.jqGrid({
    data: mydata,
    datatype: 'local',
    colModel: [
        { name:'Name', index:'Name', width:200 ,editable:true},
        { name:'Category', index:'Category', width:200,editable:true },
        { name:'Subcategory', index:'Subcategory', width:200,editable:true }
    ],
    sortname: 'Name',
    viewrecords: true,
    rownumbers: true,
    sortorder: "desc",
    cellEdit: true,
    cellsubmit: 'clientArray',
    ignoreCase: true,
    pager: '#pager',
    height: "auto",
    caption: "How to use filterToolbar better locally including local cell editing",
    afterSaveCell: function(rowid,name,val,iRow,iCol) {
        if ($.inArray(name,selectColumns) !== -1) {
            setSearchSelect(name);
        } else if ($.inArray(name,autocompleteColumns) !== -1) {
            setAutocomplete(name);
        }
    }
}).jqGrid('navGrid','#pager',
          {edit:false, add:false, del:false, search:false, refresh:true});

$.each(selectColumns,function() {
    setSearchSelect(String(this));
});

$.each(autocompleteColumns,function() {
    setAutocomplete(String(this));
});

grid.jqGrid('filterToolbar',
            {stringResult:true, searchOnEnter:true, defaultSearch:"cn"});
Community
  • 1
  • 1
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • Excellent!!!Thank you so much for understanding my requirement. It would also be great if you could help me with the 1st and 2nd questions. I have used editrules{number:true}. Though I get the incorrect value displayed in the pop up, I want to retain the incorrect value entered by the user in the textbox. Is it possible? – Sree May 12 '11 at 02:35
  • @Oleg - Your Answer is really precious and valuable. But I am toggling it to unanswered to add one more doubt to the solution provided.Sorry I tried myself to find it but couldn't. In the buildSearchSelect: method , "how can I include filter for empty string?" As explained above I have a "Add Row" button, which will add new rows to the grid. So when the user wants to filter rows with empty columns I need a empty filter value. – Sree May 13 '11 at 04:58
  • @Sree: The problem is in your `addRow`. I would not recommend you to use th scenario. Instead of that you can add **one** empty row and call `editCell` to start editing of the new row immediately. You will can not filter for empty string with respect of the toolbar searching. It is possible to filter to blank rows if you would manually fill searching parameter (for example `filters`) of `postData`. You can add new button which do this. – Oleg May 13 '11 at 07:08
  • @Oleg -The requirement for "Add Row" is 1).if the user selects already existing row(s), I need to clone the no. of times the user enters in the "addRowsInput" textbox and add it to the end 2).if no rows are selected, blank rows will be added with the one column autopopulated. Hence I used it that way. Actually I have 3"select" fields and I've included "blank/empty string" filter for them like - var saleIndStr = ":All;0:Launched;1:Sold;2:Aborted;3:" , where "3:" gives me option to filter rows with empty "sale" columns. But for our new textbox custom filters, I am not able to do that. – Sree May 13 '11 at 07:33
  • @Sree: What you explain is absolutely another question which is independend from another. You set some special requirements duplicate **many** rows at once and so have problem to filter for the new not yet filled rows. If you would add/duplicate only one row you will probebly not has any problem at all. Another way would be to include in the select an additional value "Unknown". One more possibility is to have an additional hidden row with the boolean value. The value of the hidden column for filled roew can be `true` and for new rows `false`. In the `afterSaveCell` you can change the value. – Oleg May 13 '11 at 08:29
  • @Sree: I mean that you could easy to filter for the hidden column. In any way you will have to set the filter manually. See [here](http://stackoverflow.com/questions/3981874/multiple-search-with-multiplefields-by-default/3981986#3981986) and [here](http://stackoverflow.com/questions/4492963/jqgrid-client-side-searching/4509018#4509018). – Oleg May 13 '11 at 08:37
  • @Oleg - Ok Fine. I don't want to deviate. Plz help me understand better. Leave alone "Add Row".For eg: I have something like var mydata = [ {id:"1", Name:"", Category:"", Subcategory:""}, {id:"2", Name:"", Category:"", Subcategory:""}, {id:"3", Name:"Albert Einstein", Category:"science", Subcategory:"physics"}, now the custom filter method buildSearchSelect will have values =":All;:;Science:Science", But if I want to filter rows "1" and "2" by selecting empty string from the filter drop down, how will I implement it. Sorry if I am eating your time – Sree May 13 '11 at 08:39
  • @Sree: Probably [the answer](http://stackoverflow.com/questions/5749723/jqgrid-filtering-records/5750127#5750127) can explain you all more clear. – Oleg May 13 '11 at 12:50
  • @Oleg - Thanks for your suggestions Oleg. Your last post cleared my doubt. – Sree May 16 '11 at 02:21