Yii framework: filter application + jeditable + pager style definition application instance in cdatacolumn

Source: Internet
Author: User
Tags html encode

The application environment is in the yii1.1 framework. The cdatacolumn filter application brings great convenience to cgridview. It is an important function to filter and display existing table data based on the value of a field, unfortunately, yii's Chinese materials are not detailed enough. Secondly, Baidu has made too many duplicates and has little to use. after reading a lot of English documents, I will post my own examples for your reference. If necessary, I will post some yii articles later.

 

Filter first needs to have its own data source. This can be the model used in the current table or some other arrays. In this example, I use the current model, if the filter field of cgridview is not set, the filter column is not displayed in the table. in this controll, I use a search rewrite method in the model to generate a model instance. The editkey method is used to receive the value passed by jeditable and to modify the key field with all efforts, because the returned result is very simple, only 0 and 1 are used to return the modified status.

1 Public Function actionindex () {2 if (yii: APP ()-> User-> isguest) {3 $ this-> redirect (Array ('index/login'); 4 exit; 5} 6 $ this-> select ['key'] = "current "; 7 8 $ model = new cyaskquestion ('searchgrid'); 9 $ this-> render ("Index", array ('model' => $ model )); 10 11} 12 13 14/** 15 * accept Ajax values, modify the keyword 16 * @ Param int qid17 */18 public function actioneditkey () {19 $ qid = yii :: APP ()-> request-> getparam ('qid ')! = ''? Yii: APP ()-> request-> getparam ('qid'): 0; 20 $ key = yii: APP () -> request-> getparam ('key ')! = ''? Yii: APP ()-> request-> getparam ('key'): ''; 21 if ($ qid> 0 & $ key! = '') {22 $ model = cyaskquestion: Model ()-> Find ('qid =: qid', array (': qid' => $ qid )); 23 $ model-> KEYWORDS = $ key; 24 if ($ model-> validate () & $ model-> Save () {25 exit ("1 "); 26} else {27 exit ("0"); 28} 29} 30}

 

You need to create a modified Search Method in the model. Of course, if you directly modify the search method, you can also. In many online articles about cgridview, in dataprovider, $ model-> Search () is always used to provide data. In this case, I create another method to provide data. When you click filter data, the selected filter data will be sent to controll. This filter value will be obtained when the model is created in controll. Therefore, if no filter is available, all data will be retrieved, if the filter value is available, the data is retrieved based on the value.

1/** 2 * extract the question list based on the uploaded category value 3 * @ Param int sort category 4 */5 Public Function searchgrid () {6 $ sort = yii :: APP ()-> request-> getparam ('sort '); 7 $ criteria = new cdbcriteria; 8 $ criteria-> with = 'so '; 9 10 if ($ sort> 0) {11 $ criteria-> addcondition ('t. sid1 =: Sid '); 12 $ criteria-> Params [': Sid '] = $ sort; 13} 14 return New cactivedataprovider ($ this, array (15 'Criteria '=> $ criteria, 16 'pagination' => array (17 'pagesize' => 20, 18 'pagevar' => 'page', 19 ), 20); 21}


The view page is displayed. In cgridview, you must first set the filter attribute of cgridview so that the filter column appears in the table and then define the specific data of the filter when defining each column, if this column does not require the filter function, you can disable the filter function of this column. If no class is defined in the column definition of cgridview, the default value is cdatacolumn. Therefore, filter is one of the attributes of cdatacolumn, we need to know that I have added filter to the sort column and disabled filter in other columns. filter is a drop-down box, so chtml: drodownlist is used to display data. in addition, the jeditable plug-in is used, so the key data column uses the value attribute. The attribute value is a piece of JS Code. This Code yii will be automatically returned to Js for execution, the purpose is to add a class indicator ID for the jeditable to execute the click-edit effect, so as to easily find this HTML element.

1 <? PHP 2 $ this-> widget ('zii. widgets. grid. cgridview ', array (3 'id' => 'key _ grid ', 4 // This is the table data source definition 5 'dataprovider' => $ model-> searchgrid (), 6 // if not set here, the filter column of the table does not display 'filter' => $ model, 8 // This is the table style definition. You can change it to 9 'itemscssclass '=> 'items ', 10 // because the table row style conflicts with other places, the style 11 'rowcssclass' => array ('oss alt-row ', 'even'), 12 // define the footer style of the cgridview. Many people ask how to define the footer style, so 13 'pager' => array (14 'header' is also posted here' => '', 15' maxbuttoncount '=> 7,16' firstpagecssclass '=> 'previous', 17' lastpagecssclass' => 'Next ', 18 'firstpagelabel '=>' <', 19 'lastpagelabel' => ', 20' prevpagelabel' => '<', 21 'nextpagelabel '=>', 22), 23 'columns '=> array (24 // class attributes are not defined, both of which are cdatacolumn classes by default, 'filter' => false, that is, 25 array ('name' => 'qid', 'header' => 'id ', 'filter' => false, 'htmloptions '=> array ('style' => 'Text-align: Center'), 26 array ('name' => 'aktime', 'header' => 'posting time ', 'value' => 'date ("Y-m-d", $ data-> asktime) ', 'filter' => false, 'htmlopexception' => array ('style' => 'text-align: Center'), 27 array ('name' => 'so. sort1 ', 'header' => 'category', 28 // The filter in the sort column is not closed. The sort data in the cyasksort category table is used, in fact, you can write a method in the cyasksort model to directly return the data of listdata, Which is concise and clear. for everyone to understand, we specifically post the data source method 29 'filter' => chtml: dropdownlist ('sort ',' ', Chtml: listdata (cyasksort: Model ()-> findall ("sid1 = 0"), 'sid', 'sort1 ')), 'htmlopexception' => array ('style' => 'text-align: Center'), 30), 31 array ('name' => 'title ', 'header' => 'title', 'filter' => false, 'htmloptions '=> array ('style' => 'text-align: center; width: 600px '), 32 // This column applies jeditable. The purpose of the definition in value is to return a div whose class is equal to editable. The title attribute is used if the modification fails, this attribute is extracted and restored to the original data. The type attribute is very important. Raw indicates that the value attribute of this column is not used. The HTML encode is URL encoded. Otherwise, <div> </div> is displayed in the data column. 33 array ('name' => 'keyword', 34 'header' => 'keyword ', 35 'filter' => false, 36 'htmloptions '=> array ('style' => 'text-align: center; width: 400px; Height: 25px '), 37 'value' => function ($ data) {38 return '<Div class = "edittable" id = '. $ data-> qid. 'title = "'. $ data-> keywords. '"> '. $ data-> keywords. '</div>'; 39}, 40 'type' => 'Raw' 41), 42), 43 // afterajaxupd The ate attribute can be used to obtain data after the filter action, and then perform some JS actions. This is not required in this example. However, this attribute is very important for some extended functions, so it is pasted out, you can rewrite it as needed. 44 'afterajaxupdate' => 'function () {}', 45) 46?> 47 <! -- Introduce the table editing plug-in --> 48 <? PHP yii: APP ()-> clientscript-> registerscriptfile (yii: APP ()-> request-> baseurl. '/JS/jquery. jeditable. js');?> 49 <SCRIPT type = "text/JavaScript"> 50 $ ("Div [class ^ = 'edittable']"). editable (function (value, settings) {51 // the row to be modified depends on this ID, this is the 52 id = $ (this) provided in the <Div class = "editable"> element ). ATTR ("ID"); 53 // This is the original value. The value in this method is the modified value 54 orikey = $ (this ). ATTR ('title'); 55 $. ajax ({56 type: "Post", 57 // If token verification is enabled in yii, you must upload the csrftoken together. Otherwise, 58 data: "Key =" + value + "& qid =" + ID + "<? PHP echo '&'. yii: APP ()-> request-> csrftokenname. "=". yii: APP ()-> request-> getcsrftoken ()?> ", 59 URL:" <? PHP echo $ this-> createurl ("key/editkey")?> ", 60 async: false, 61 success: function (data) {62 if (Data! = '1') {63 alert ('modification failed'); 64 // if the modification fails, the original value is returned. Otherwise, the modified value 65 value = orikey is returned; 66} 67} 68}) 69 return value; 70 71}, {72 // This is the edit Column Style definition of edittable, there are many parameters, for more information, see 73 submit: "OK", 74 width:}); 76 </SCRIPT>


In addition, cgridview uses the jquery class library. If you manually call jquery elsewhere on the page, a conflict error may occur, as shown in figure

$. Param. querystring is not a function. Therefore, no matter how you select filter, you will not send a request channel cintroll to retrieve data. The solution is to call jquery wherever you are, it is best to use registe of yii for registration instead of <SCRIPT>.

 

Yii framework: filter application + jeditable + pager style definition application instance in cdatacolumn

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.